public with sharing class AccountNewOverrideController {
    Profile profile;
    string defaultrecordTypeId;
    string recordTypeAASMasterClient   = RecordTypeHelper.GetRecordTypeId('Account', 'AAS Master Client');
    string recordTypeAASAgency         = RecordTypeHelper.GetRecordTypeId('Account', 'AAS Agency');
    string recordTypeAASSalesAccount   = RecordTypeHelper.GetRecordTypeId('Account', 'AAS Sales Account');
    
    string returl ='/001/o';
    
    public AccountNewOverrideController(ApexPages.StandardController controller) {
        Init();
           
    }
    
    private void Init(){
            this.profile=[Select p.Name From Profile p where p.Id=:UserInfo.getProfileId()];            
            RecordTypeHelper.SetRecordTypeIncaseNotSet('Account');
            defaultrecordTypeId= ApexPages.currentPage().getParameters().get('RecordType');
            System.Debug('Pname->' +  profile.Name + ApexPages.currentPage().getParameters().get('RecordType'));
    }
    
    public PageReference doRedirect() {
    
          //************ custom redirect ****************************
          
          
       
          if (profile.Name == 'AAS Operations Team'  || profile.Name == 'Advanced Ad Solutions (Delegated Admin)' || profile.Name == 'Advanced Ad Solutions (Client Services)' ) {
                   //System.Debug('recordTypeAASNationalIngenioAccount' + recordTypeAASNationalIngenioAccount);
                  //Not allowed to create the below record types
                  
                  //if AAS Agency , AAS Master Client-redirect to account create page
                  if(recordTypeAASMasterClient.Contains(defaultrecordTypeId) || recordTypeAASAgency.Contains(defaultrecordTypeId)) {
                      string acctPageUrl = '/001/e?retURL=' + returl  +'&RecordType=' + defaultrecordTypeId  + '&ent=Account&nooverride=1';
                      PageReference acctPage = new PageReference(acctPageUrl );
                      acctPage.setRedirect(true);
                      return acctPage;
                  }
                   //if AAS Sales Account, redirect to opp wizard
                   else if(recordTypeAASSalesAccount.Contains(defaultrecordTypeId)  ) {
                       PageReference p = Page.SelectMasterClient;
                       p.setRedirect(true);
                       p.getParameters().put('retUrl',returl  );
                       p.getParameters().put('ent', 'Account');
                       p.getParameters().put('RecordType',defaultrecordTypeId );
                       return p;
                   }
                  else {//all other record types ,error page
                     System.Debug('**second1');
                     string feedbackUrl = '/apex/feedback?returl=' + returl + '&txt=You do not have permission(s) to create Account of selected Record Type.';
        
                     PageReference feedbackPage = new PageReference(feedbackUrl );
                     feedbackPage .setRedirect(true);
                     return feedbackPage ;             
        
                  } 
          }
      
               
     
          //************ default new account redirect  ****************************
          string  acctPageUrl = '/001/e?retURL=%2F001%2Fo&RecordType=' + defaultrecordTypeId  + '&ent=Account&nooverride=1';
          PageReference acctPage = new PageReference(acctPageUrl );
          acctPage.setRedirect(true);
          return acctPage;
        
      }
    
    
 
 

}